Skip to content

feat: add annotation support - #35

Merged
Julian merged 6 commits into
bowtie-json-schema:mainfrom
VIDIT45AGARWAL:support-annotation-output
Dec 3, 2026
Merged

Julian merged 6 commits into
bowtie-json-schema:mainfrom
VIDIT45AGARWAL:support-annotation-output

Conversation

@VIDIT45AGARWAL

Copy link
Copy Markdown
Contributor

This adds annotation support in networknt harness.

@VIDIT45AGARWAL

Copy link
Copy Markdown
Contributor Author

@stevehu would love to get your review in this.

@stevehu

stevehu commented Oct 22, 2026

Copy link
Copy Markdown

The harness compiles and preserves flag output, but I found one blocking issue with annotation locations.

[P1] Append the annotation keyword to the schema location — BowtieJsonSchemaValidator.java:205–208

Networknt’s LIST output identifies the containing subschema, whereas Bowtie requires the location of the annotation keyword itself. With {"title":"Root"}, this returns keywordLocation: "#" instead of "#/title"; absolute locations likewise omit /title.

The suffix removal also corrupts legitimate subschema paths: {"properties":{"title":{"title":"Nested"}}} reports #/properties instead of #/properties/title/title for the nested annotation.

These cases were reproduced against the pinned 3.0.6 dependency and will fail Bowtie’s location-based annotation comparisons. Please append / plus the JSON Pointer-escaped annotation keyword (~ → ~0, / → ~1) to the subschema location rather than stripping it, for both relative and absolute locations. Regression coverage should include root annotations, absolute locations, and a property whose name matches the annotation keyword.

@VIDIT45AGARWAL

Copy link
Copy Markdown
Contributor Author

Thanks for the review @stevehu
I've updated the harness to address this:

  1. Replaced the suffix-stripping logic with an appendKeywordLocation helper that properly appends / plus the JSON Pointer-escaped keyword (~ → ~0, / → ~1) to the containing subschema location.
  2. Formatted locations as absoluteKeywordLocation when the schema has an $id (absolute URI) and keywordLocation when it is a root fragment (#/...), which Bowtie now automatically resolves.

@VIDIT45AGARWAL

Copy link
Copy Markdown
Contributor Author

Sorry to ping you again, @stevehu We’re almost done with the harness updates and are hoping to get the PR merged so we can generate the complete annotation report. Would really appreciate your feedback or any follow-up suggestions when you get a chance.

@stevehu

stevehu commented Nov 30, 2026

Copy link
Copy Markdown

Thanks for the update. The earlier location issue is fixed. I compiled this head (8aa748a) against the pinned networknt 3.0.6 and ran IHOP requests through it. Root (#/title), $id roots, embedded and Draft 7 $ids, registry schemas, $anchor, $defs refs and a property named title (#/properties/title/title) now all come out where Bowtie expects them, and flag output is unchanged.

There are two more location problems, both in appendKeywordLocation, plus a few smaller notes.

Please fix before merging

1. A trailing / is an empty pointer token, not a separator (lines 202–204)
The subschema for a property named "" is #/properties/. The endsWith("/") branch turns it into #/properties/title, which also collides with a real property named title.
Reproduced: schema {"properties":{"":{"title":"Empty"}}}, instance {"":1}. Bowtie expects #/properties//title.
Fix: drop that branch and always append "/" + escaped when the location has a fragment. The root # already gives #/title.

2. Locations aren't percent-encoded, but Bowtie decodes them (line 215)
Bowtie runs urllib.parse.unquote on every reported location and on the suite's expected keys. networknt gives raw pointer tokens, so a token that contains a %XX-like sequence is decoded into a different name.
Reproduced: {"properties":{"a%41":{"title":"Pct"}}} reports #/properties/a%41/title. Bowtie decodes that to #/properties/aA/title, while the expected key (a%2541 in the suite) decodes to #/properties/a%41/title.
Bowtie's schema also describes keywordLocation as a URI-fragment JSON Pointer, and raw characters such as ^ or a space aren't valid there.
Fix: percent-encode each token of the fragment, % included, not just the appended keyword.

Library side (networknt)

3. minContains/maxContains are reported at #/contains/minContains and #/contains/maxContains. They should be at #/minContains and #/maxContains. The cause is ContainsValidator, which appends these keywords to the contains location, so I'll fix it on the networknt side. The current vendored suite doesn't assert on these keywords.

4. 3.0.6 misses several suite annotations. At 3.0.6, boolean, array and null annotation values are dropped, and readOnly/writeOnly produce no annotation.
Example: {"examples":["Foo","Bar"],"deprecated":true,"default":null,"readOnly":true} on 1 returns annotations: [].
networknt/json-schema-validator#1376 fixes these, as well as unevaluated* results changing when annotations are collected. Once it's released, bumping harnessImplementation.version in build.gradle will keep the report from showing failures the library has already fixed.

Minor

5. No regression coverage for the location mapping. A small test of appendKeywordLocation and extractAnnotations, or annotation cases in CI, would catch these. Useful cases: #, an $id root, properties/title/title, an empty property name, and %, ~ and / in names.

6. Any output other than "annotations" gets flag results. Switching on the value would be safer: "flag" (or missing) gives flag output, "annotations" works as now, and anything else gets a skipped result with a message. The implementers docs say a harness should skip a format it can't produce.

@VIDIT45AGARWAL

Copy link
Copy Markdown
Contributor Author

Thanks for the review @stevehu. I've updated appendKeywordLocation to preserve empty property tokens (#/properties//title) and percent-encode all fragment tokens, switched output handling to skip unsupported formats, and added JUnit regression tests in BowtieJsonSchemaValidatorTest.java (run in Docker via gradle test).

@stevehu

stevehu commented Dec 1, 2026

Copy link
Copy Markdown

Thanks for the update. Both location issues from my last review are fixed at bf0b077. An empty property name now gives #/properties//title, and every fragment token is percent-encoded (a%41 gives a%2541). Unsupported output values are skipped, and the new tests cover these cases.

I built the Dockerfile's builder stage, and gradle test jar passes. I also sent about 35 edge-case requests through the harness against the pinned 3.0.6. Locations came out right for:

  • $anchor and registry anchors
  • embedded, urn: and dot-segment $ids
  • $dynamicRef and $recursiveRef
  • draft-04/07 id: "#foo"
  • $ref into %-named $defs
  • non-ASCII names, and ~ or / in names

Flag output is unchanged.

Nothing below blocks merging. Two items are small test gaps. The other two are networknt bugs, which I'll fix on the library side.

Minor (harness)

1. The anchor branch of appendKeywordLocation is untested, and its output isn't a valid location (lines 256–263)
When the fragment doesn't start with /, #foo becomes #foo/title. That isn't the URI-fragment JSON Pointer that Bowtie's annotations schema requires. On this path networknt also appends tokens without ~0/~1 escaping, so splitting on / can break a name apart.
I couldn't reach this branch through $anchor, draft-04/07 id: "#foo" or registry anchors, because networknt resolves all of those to pointer locations.
Suggestion: add a test for an input that reaches it, or drop the branch if no input does.

2. The end-to-end test never asserts instanceLocation
endToEndAnnotationsLocationsAndOutputDispatch only reads keywordLocation. instanceLocation is only checked in the unit test, where it's set by hand. Bowtie groups annotations by instance location, so a change there would break every comparison and the tests in the Docker build wouldn't catch it.
Suggestion: assert it in the end-to-end case too, e.g. /a~0b~1c for a~b/c, / for the empty name and "" for the root title.

Library side (networknt)

3. Control characters in property names give wrong locations.
PathType.JSON_POINTER rewrites \n, \t, \r, \b and \f as two-character escapes (backslash + letter). The harness then percent-encodes the backslash.
Reproduced: {"properties":{"a\nb":{"title":"NL"}}} on {"a\nb":1} reports keywordLocation: "#/properties/a%5Cnb/title". Its instanceLocation has a literal backslash-n where the newline should be. The expected location is #/properties/a%0Ab/title.
The harness can't safely undo this, since a property literally named a\nb (with a real backslash) produces the same string. I'll fix it in networknt.

4. The prefixItems annotation is a count, not the largest index.
Reproduced: {"prefixItems":[{"title":"P0"}],"items":{"title":"It"}} on [1,2] gives prefixItems: 1. 2020-12 defines the value as the largest index the keyword applied a subschema to, which is 0 here. The cause is PrefixItemsValidator, which emits the number of tuple schemas. I'll fix it in networknt, along with unevaluatedItems, which reads that value.

The minContains/maxContains locations and the unevaluated* validity change from my last review are still open on the networknt side. Bumping harnessImplementation.version once those fixes are released will keep the report from showing failures the library has already fixed.

@VIDIT45AGARWAL

Copy link
Copy Markdown
Contributor Author

Thanks for checking those edge cases @stevehu. I've dropped the unreachable non-pointer fragment branch in appendKeywordLocation (now rejecting non-/ fragments with IllegalArgumentException) and added instanceLocation assertions to endToEndAnnotationsLocationsAndOutputDispatch.

@stevehu

stevehu commented Dec 2, 2026

Copy link
Copy Markdown

Thanks, both items are fixed at d23d78e. A non-pointer fragment now throws, so it would come back as an errored case instead of a wrong location, and a unit test covers it. The end-to-end test now checks instanceLocation, and the values match what the harness emits against 3.0.6. All 5 tests pass.

One small thing in the new test, which doesn't block merging:

titleLocations keeps only the last patternProperties annotation (BowtieJsonSchemaValidatorTest.java, around line 196)
The map is keyed by keywordLocation, but ^a matches a%41, a~b/c and apple. So three annotations share #/patternProperties/%5Ea/title, and each put overwrites the previous one. At 3.0.6 they arrive as /a%41, /a~0b~1c, /apple, so the /apple assertion passes only because that one comes last. If a networknt bump changes the order, the test fails even though the output is correct. If one of the other two goes missing, it still passes.
Suggestion: collect (keywordLocation, instanceLocation) pairs in a Set and assert all three patternProperties pairs.

The two library-side issues from my last comment, control characters in names and the prefixItems value, are still on my list for networknt.

@VIDIT45AGARWAL

Copy link
Copy Markdown
Contributor Author

Good catch on the map overwrite @stevehu. Updated endToEndAnnotationsLocationsAndOutputDispatch to assert the full Set of (keywordLocation, instanceLocation) pairs so all three patternProperties matches are checked order-independently.

@stevehu

stevehu commented Dec 3, 2026

Copy link
Copy Markdown

Thanks, that fixes it. At f0ff3bd the test compares all eight (keywordLocation, instanceLocation) pairs as a set, so it no longer depends on order and checks all three patternProperties matches. All 5 tests pass. Nothing else is needed on the harness side from me.

I also ran the JSON-Schema-Test-Suite annotation tests through the harness against 3.0.6, comparing the output the way Bowtie does:

Dialect Passed
2020-12 41/52
2019-09 33/43
draft-07 20/24
draft-06 14/16
draft-04 13/13

Every failure traces to networknt, and the harness's location mapping was correct in all of them. These will show up as failures in the report once this merges. I'll fix them on the library side:

  1. Annotations under a failed subschema aren't dropped. Only the unit that failed has its annotations dropped; passing subschemas under it keep theirs. For example, {"anyOf":[{"properties":{"a":{"title":"A"}},"required":["z"]},{"title":"B"}]} on {"a":1} reports #/anyOf/0/properties/a/title. The same happens through oneOf, if, not and $ref. Suite: unevaluatedProperties with not and unevaluatedItems with not.
  2. Boolean, array and null annotation values are dropped. This affects deprecated: true, examples: [...], and a default of false, [1] or null.
  3. readOnly and writeOnly produce no annotation.
  4. contentMediaType, contentEncoding and contentSchema annotate non-string instances on 2019-09 and 2020-12. contentSchema also annotates when there's no contentMediaType.
  5. propertyNames annotations are reported at the property value's location (/foo). Suite: propertyNames doesn't annotate property values.

These are in addition to the networknt items from my earlier comments: control characters in names, the prefixItems value, the minContains/maxContains locations and the unevaluated* validity change.

@VIDIT45AGARWAL

Copy link
Copy Markdown
Contributor Author

Thanks for the review @stevehu. I think this PR is ready @Julian.

@Julian
Julian merged commit 378990a into bowtie-json-schema:main Dec 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants